2
2
.
.
9
9
.
.
7
7
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
-
-
@
@
P
P
r
r
o
o
f
f
i
i
l
l
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
Another way to tell Spring which Implementation/Class to inject is to use @Profile Annotation.
Profiles define which Class will be used in your application.
You can assign one or more Profiles to a Class.
Then in the configuration file you define Active Profile.
When you start application only Classes that belong to the Active Profile (or have no @Profile) will be used.
Syntax
@Profile( "Profile1" )
@Profile({"Profile2","Profile3"})
public class MyServiceImplementation1 implements MyServiceInterface { ... }
application.properties
spring.profiles.active = Profile1
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
MyController
MyServiceInterface
@Profile("Profile1")
MyServiceImplementation1
http://localhost:8080/Hello
hello()
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_autowired_profile (add Spring Boot Starters from the table)
Edit File: application.properties (spring.profiles.active = Profile1)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Interface: MyServiceInterface.java (inside package controllers)
– Create Class: MyServiceImplementation1.java (inside package controllers)
– Create Class: MyServiceImplementation2.java (inside package controllers)
application.properties
spring.profiles.active = Profile1
MyServiceInterface.java
package com.ivoronline.springboot_autowired_profile.services;
public interface MyServiceInterface {
public String sayHello();
}
MyServiceImplementation1.java
package com.ivoronline.springboot_autowired_profile.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("Profile1")
public class MyServiceImplementation1 implements MyServiceInterface {
public String sayHello() {
return "Hello";
}
}
MyServiceImplementation2.java
package com.ivoronline.springboot_autowired_profile.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile({"Profile2","Profile3"})
public class MyServiceImplementation2 implements MyServiceInterface {
public String sayHello() {
return "Hello World";
}
}
MyController.java
package com.ivoronline.springboot_autowired_profile.controllers;
import com.ivoronline.springboot_autowired_profile.services.MyServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
MyServiceInterface myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>